<?php defined('SYSPATH') or die('No direct script access.');
/**
 * 会员管理
 *
 * @author  Yellow.Chow <aultoale@gmail.com>
 * @version $Id: member.php 277 2012-06-27 08:58:47Z yellow.chow $
 * @license http://www.gnu.org/licenses/gpl-3.0.html    GPL 3
 */
class Controller_Admin_Member extends Controller_Admin_Base
{

    /**
     * 会员列表
     */
    public function action_index()
    {
        $this->view->member_id = (int) $this->request->query('member_id');
        $this->view->email     = (string) $this->request->query('email');
        $this->view->nickname  = (string) $this->request->query('nickname');
        $this->view->reg_time  = strtotime($this->request->query('reg_time'));
        $this->view->status    = (string) $this->request->query('status');

        $autosort = $this->autosort('member_id', 'desc');

        $sql = Model::factory('member')->all_sql()
            ->order_by($autosort->field, $autosort->direct);

        if ($this->view->member_id)
        {
            $sql->where('member_id', '=', $this->view->member_id);
        }

        if ($this->view->email)
        {
            $sql->where('email', 'LIKE', '%'.$this->view->email.'%');
        }

        if ($this->view->reg_time)
        {
            $sql->where('reg_time', '>=', $this->view->reg_time);
        }

        if (is_numeric($this->view->status))
        {
            $sql->where('status', '=', $this->view->status);
        }

        $this->view->pagination = new Pager($sql);
    }

    /**
     * 修改会员信息
     */
    public function action_profile()
    {
        $member_id = $this->request->param('id');
        $model = Model::factory('member');
        $member = $model->get($member_id);

        if ( ! $member)
        {
            $this->message('admin.member.not_found')
                ->link(URL::site('admin/member'), 'admin.member.index_url')
                ->render();
        }

        if ($this->request->is_post())
        {
            if ( ! $model->profile($member_id, $this->request->post()))
            {
                $this->view->errors = $model->errors('admin.member');
            }
            $this->render('admin/common/message');
        }

        $this->view->set($member);
    }

}